package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityMonitorUserConfig;
import com.loongtech.bi.entity.system.EntityMonitorUserGroup;
import com.loongtech.bi.manager.system.BiMonitorGroupConfigManager;
import com.loongtech.bi.manager.system.BiMonitorUserConfigManager;
import com.loongtech.bi.manager.system.BiMonitorUserGroupManager;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;

@Controller
@Scope("view")
public class BiMonitorUserConfigAction {
	@Resource
	BiMonitorUserConfigManager biMonitorUserConfigManager;
	@Resource
	BiMonitorGroupConfigManager biMonitorGroupConfigManager;
	@Resource
	BiMonitorUserGroupManager biMonitorUserGroupManager;

	private List<String> typeList;
	private EntityMonitorUserConfig entityMonitorUserConfig;
	private List<EntityMonitorUserConfig> userList;

	@Autowired
	private void inital() {
		typeList = new ArrayList<>();
		typeList.add("微信");
		typeList.add("手机号");
		typeList.add("语音号");
		typeList.add("邮箱");

		userList = biMonitorUserConfigManager.queryList();
		entityMonitorUserConfig = new EntityMonitorUserConfig();
	}

/*	public String[] transformSelectedItem(String str) {

		if (str == null || str.length() == 0) {
			return new String[0];
		}

		String[] strArray = str.split("-");
		return strArray;
	}*/

	public void searchByCriteria() {
		userList = biMonitorUserConfigManager.searchByCriteria(entityMonitorUserConfig);
	}

	/**
	 * 增加
	 */
	public void addUser() {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		if (validate(faceContext, entityMonitorUserConfig)) {
			biMonitorUserConfigManager.addUser(entityMonitorUserConfig);
			faceContext.addMessage(null, new FacesMessage("提示", "添加成功！"));
			inital();
		}
	}

	/**
	 * 删除
	 */
	public void delUser(String id) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		EntityMonitorUserGroup userGroup = biMonitorUserGroupManager.getByQuery("from EntityMonitorUserGroup where userId = ?", Integer.parseInt(id));
		if (userGroup == null) {
			biMonitorUserConfigManager.delUser(Integer.parseInt(id));
			faceContext.addMessage(null, new FacesMessage("提示", "删除成功！"));
		} else {
			String groupName = biMonitorGroupConfigManager.getByQuery("select groupName from EntityMonitorGroupConfig where id = ?", userGroup.getGroupId());
			faceContext.addMessage(null, new FacesMessage("删除失败", "用户有所属分组：" + groupName));
		}
		inital();
	}

	/**
	 * 修改
	 */
	public void modifiyUser(EntityMonitorUserConfig e) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		if (validate(faceContext, e)) {
			biMonitorUserConfigManager.modifiyUser(e);
			faceContext.addMessage(null, new FacesMessage("提示", "修改成功！"));
			inital();
		}
	}

	private boolean validate(FacesContext faceContext, EntityMonitorUserConfig entity) {
		if (Utils.isEmptyStr(entity.getUserName())) {
			faceContext.addMessage(null, new FacesMessage("提示", "用户名不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(entity.getWeChat())) {
			faceContext.addMessage(null, new FacesMessage("提示", "微信号不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(entity.getPhoneNum())) {
			faceContext.addMessage(null, new FacesMessage("提示", "手机号不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(entity.getVoiceNum())) {
			faceContext.addMessage(null, new FacesMessage("提示", "语音号不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(entity.getEmail())) {
			faceContext.addMessage(null, new FacesMessage("提示", "邮箱不能为空！"));
			return false;
		}
		return true;
	}


	// 数组转字符串，以"-"分割
	// private String array2String(String[] strs) {
	// if (strs == null || strs.length == 0) {
	// return "";
	// }
	// StringBuilder sb = new StringBuilder();
	// for (int i = 0; i < strs.length - 1; i++) {
	// sb.append(strs[i]);
	// sb.append("-");
	// }
	// sb.append(strs[strs.length - 1]);
	// return sb.toString();
	// }

	public List<String> getTypeList() {
		return typeList;
	}

	public EntityMonitorUserConfig getEntityMonitorUserConfig() {
		return entityMonitorUserConfig;
	}

	public void setEntityMonitorUserConfig(EntityMonitorUserConfig entityMonitorUserConfig) {
		this.entityMonitorUserConfig = entityMonitorUserConfig;
	}

	public List<EntityMonitorUserConfig> getUserList() {
		return userList;
	}

	public void setUserList(List<EntityMonitorUserConfig> userList) {
		this.userList = userList;
	}

	public void setTypeList(List<String> typeList) {
		this.typeList = typeList;
	}
}
